建立商品列表及新增商品、刪除商品Controller
public ActionResult ItemBlock(int Id)
{
ItemDetailView ViewData = new ItemDetailView();
ViewData.Data = itemService.GetDataById(Id);
string Cart = (HttpContext.Session["Cart"] != null)
? HttpContext.Session["Cart"].ToString() : null;
ViewData.InCart = cartService.CheckInCart(Cart, Id);
return PartialView(ViewData);
}
[Authorize(Roles = "Admin")]
public ActionResult Create()
{
return View();
}
[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult Add(ItemCreateView Data)
{
if (Data.ItemImage != null)
{
string filename = Path.GetFileName(Data.ItemImage.FileName);
string Url = Path.Combine(Server.MapPath("~/Upload/")
, filename);
Data.ItemImage.SaveAs(Url);
Data.NewData.Image = filename;
itemService.Insert(Data.NewData);
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("ItemImage", "請選擇上傳檔案");
return View(Data);
}
}
[Authorize(Roles = "Admin")]
public ActionResult Delete(int Id)
{
itemService.Delete(Id);
return RedirectToAction("Index");
}
}